﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvantGameMachine.States.InternalObjects;

namespace AvantGameMachine.States
{
    internal class StateManager
    {
        private List<StatesProvider> providers = new List<StatesProvider>();
        private List<StatesProvider> providersToAdd = new List<StatesProvider>();
        private bool AvoidAdding = false;


        internal StateManager()
        {

        }

        internal void RegisterStateProvider(StatesProvider _newProvider,string _firstState)
        {
            if (AvoidAdding)
            {
                providersToAdd.Add(_newProvider);
            }
            else
            {
                providers.Add(_newProvider);
            }
            _newProvider.ChangeState(_firstState);
        }

        internal void Update()
        {
            if (providers.Count > 0)
            {
                AvoidAdding = true;
                foreach (StatesProvider sp in providers)
                {
                    if (sp.currentState != string.Empty)
                        sp.Update(sp.currentState);
                }
                AvoidAdding = false;
            }

            if (providersToAdd.Count > 0)
            {
                foreach (StatesProvider sp in providersToAdd)
                {
                    providers.Add(sp);
                }
                providersToAdd.Clear();
            }
        }

        internal SystemStatesProvider GetSSP()
        {
            return (SystemStatesProvider)providers[0];
        }
    }
}
